import json

import battlefield_domain
import battlefield_data

class stupid(object):
    def __init__(self, ui):
        self.ui = ui
        self.data_source = battlefield_data.data("http://10.20.134.108:8000", 
                                                 "http://10.20.134.108:8001")
        self.ui.print_info("data init")
        self.state = 1

    
    def sync_player(self):
        self.player = self.data_source.get_player(self.player.name)
        self.ui.print_info("You - " + 
                           "life: " + str(self.player.life) + 
                           " power: " + str(self.player.power))

    def apply_actions(self):
        while True:
            try:
                a = self.data_source.get_action(self.player.name)
                if 0 == a.type: # attack me!
                    self.player.life = self.player.life - a.power
                self.data_source.update_player(self.player)
            except: # no actions
                break

    def is_dead(self):
        return self.player.life <= 0

    def player_list(self):
        player_list = self.data_source.get_player_list()
        list = []
        for name in player_list:
            player = self.data_source.get_player(name)
            if player.life <= 0:
                life = "dead"
            else:
                life = str(player.life)
            list.append({"name":name, "life":life})
            
        self.ui.print_info("players: " + json.write(list))
    
    def loop(self): # start state machine
        while True:
            if 0 == self.state:
                return True
            elif 1 == self.state:
                self.state = self.state1()
            elif 2 == self.state:
                self.state = self.state2()
            elif 3 == self.state:
                self.state = self.state3()
            elif 4 == self.state:
                self.state = self.state4()
            elif 5 == self.state:
                self.state = self.state5()
            elif 100 == self.state:
                self.state = self.state100()
    
    def state1(self):
        choice = self.ui.print_menu(["1. start game", 
                                     "2. quit"], 
                                     ["1", "2"])
        if "1" == choice:
            return 2
        else:
            if "2" == choice:
                return 0
    
    def state2(self):
        self.ui.print_msg("your name")
        name = self.ui.promt()
        try:
            self.player = self.data_source.get_player(name)
            self.apply_actions()
            if self.is_dead():
                return 100
        except:
            self.player = battlefield_domain.player(name)
            try:
                self.data_source.create_player(self.player)
                self.data_source.enter_battlefield(name)
            except:
                self.ui.print_error("cannot connect to server")
                return 1
        return 3
    
    def state3(self):
        self.sync_player()
        self.apply_actions()
        self.data_source.update_player(self.player)
        self.player_list()
        if self.is_dead():
            return 100
        
        choice = self.ui.print_menu(["1. List all persons", 
                                     "2. Attack a person",
                                     "3. Quit"], 
                                     ["1", "2", "3"])
        if "1" == choice:
            return 5
        elif "2" == choice:
            return 4
        elif "3" == choice:
            return 0
    
    def state4(self):
        if self.is_dead():
            return 100
        self.ui.print_info("who ?")
        self.player_list()
        name = self.ui.promt()
        action = battlefield_domain.action(0, self.player.power)
        try:
            self.data_source.send_action(action, name)
        except:
            self.ui.print_error("cannot connect to server")
            return 0
        return 3
    
    def state5(self):
        self.player_list()
        return 3

    def state100(self):
        self.ui.print_info("You are killed")
        return 0
